package com.example.ywj.toutiao.utils;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.Title;

import java.util.List;

public class MyGridViewAdapter extends BaseAdapter {
    private static final String TAG = "MyGridViewAdapter";
    public List<Title> list;
    public MyGridViewAdapter(List<Title> list){
        this.list = list;
        //Log.i(TAG, "MyGridViewAdapter: "+list);
    }
    @Override
    public int getCount() {
        Log.i(TAG, "getCount: "+list.size());
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i(TAG, "getView: "+position);
        ViewHolder viewHolder ;
        if(convertView == null){
            //获取布局管理器
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.mytitle_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.tv = convertView.findViewById(R.id.mytitle_title);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        Log.i(TAG, "getView: "+list.get(position).getTitle());
        viewHolder.tv.setText(list.get(position).getTitle());
        return convertView;
    }
    class ViewHolder{
        TextView tv;
    }
}
